package ssi.actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import ssi.beans.User;
import ssi.tools.Connector;
import ssi.tools.SendEmail;
import ssi.tools.Tool;


public class RegisterAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
		throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		String target = null;
		User user = (User) form;
		String email = user.getUserName();
		String password = user.getPassword();
		String confirmPassword = request.getParameter("confirmPassword");
		String name=user.getName();
		String surname=user.getSurname();
		String address=user.getAddress();
		String pesel=user.getPesel();
	
		ActionErrors actionErrors = new ActionErrors();
		if(!(name.equals("") || surname.equals("") || address.equals("") || pesel.equals("") || email.equals("") || password.equals("") || confirmPassword.equals(""))){
			if(password.equals(confirmPassword)){
				if(Tool.checkPeselFormat(pesel)){
					try{
			    		Connection connection = new Connector().getConnection();
			    		Statement statement = connection.createStatement();
			    		String query = "SELECT COUNT(*) FROM `users` WHERE `email`='"+email+"'";
				    	ResultSet rs = statement.executeQuery(query);
				    	rs.next();
			    		if(rs.getInt("COUNT(*)")==0){
			    			long psl=Long.parseLong(pesel);
			    			statement.executeUpdate("INSERT INTO `users`(`name`, `surname`, `address`, `pesel`, `email`,`password`, `activation` ) VALUES(\""+name+"\",\""+surname+"\",\""+address+"\",\""+psl+"\",\""+email+"\",\""+DigestUtils.md5Hex(password)+"\",\""+DigestUtils.md5Hex(password)+"\");");
			    			new SendEmail().sendActivationEmail(email,DigestUtils.md5Hex(password));
			    			target="success";
		
			    			rs.close();
		    				connection.close();
			    		}else{
			    			
			    			target = "failure";
							actionErrors.add("registerError",
									new ActionMessage("error.email"));
							rs.close();
							connection.close();
			    		}
					}catch(Exception e){
			
						target = "failure";
						actionErrors.add("registerError",
								new ActionMessage("error.catch"));
						
					}
				}else{
					
					target = "failure";
					actionErrors.add("registerError",
							new ActionMessage("error.pesel"));
				}
			}else{
			
				target = "failure";
				actionErrors.add("registerError",
						new ActionMessage("error.passwordEquals"));
			}
		}else{
		
			target = "failure";
			actionErrors.add("registerError",
					new ActionMessage("error.emptyFields"));
		}
		if(actionErrors.size() !=0){
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		System.out.println(actionErrors);
	
		
		return mapping.findForward(target);
	}

	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}
	

	

}
